package com.baosight.sczx.zm.cg.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cg.DailyReport;
import com.baosight.sczx.common.zm.cInterface.cp.ProducePlanCM;
import com.baosight.sczx.common.zm.cInterface.cs.WorkSchedule;
import com.baosight.sczx.zm.cg.domain.ZMCGMineReportFinal;

/**
 * 采煤进度汇报（调度日报用）
 * <p>Title: ServiceZMCG83.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张志芳
 * @version 1.0
 * <pre>Histroy:
 *          2011-9-6 下午02:24:59  张志芳  Create
 *</pre>
 */
public class ServiceZMCG83 extends ServiceEPBase {
	/**
	 * 初始化
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCP70</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-6下午03:53:52 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的		
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	
	/**
	 * 查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCP70</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-6下午03:58:03 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		String fdMiningId = (String) UserSession.getInSessionProperty("miningCode");//矿ID的默认值
		String queryDate = inInfo.getString("querydate");
		
		//调度日报模块，默认刚进来传值
		if (queryDate == null || queryDate.trim().equals("")) {
			queryDate = StaticTools.getDate();
			inInfo.set("querydate", queryDate);
		}
		StaticTools.setQueryParameters(inInfo, "fdMiningId", fdMiningId, true);
		StaticTools.setQueryParameters(inInfo, "fdVirtualDate", queryDate, true);
		EiInfo outInfo = super.query(inInfo, "ZMCGMineReportFinal.query", new ZMCGMineReportFinal());
		String tempDate = StringUtils.isNotBlank(inInfo.getString("querydate")) ? inInfo.getString("querydate") : StaticTools.getDate();
		outInfo.addBlock(ProducePlanCM.getWorkingFaceBlock("cmes.cg.workingface", fdMiningId, StaticTools.getYearStr(tempDate), null, null, true));
		outInfo.addBlock(WorkSchedule.getWorkScheduleBlock("cmes.cg.workNum", fdMiningId, true));//班次
		outInfo.addBlock(StaticBlocks.getBlockData("cmes.cg.status", new String[]{"草拟", "上报"}, new String[]{"草拟", "上报"}, true));//状态
		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cg.department", "orgCode", "orgName", true, fdMiningId));
		return outInfo;
	}
	
	/**
	 * 新增
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCP70</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-6下午03:58:15 张志芳
	 * </p>
	 *
	 */ 
	@Override
	public EiInfo insert(EiInfo inInfo) {	
		EiInfo out=new EiInfo();
		ZMCGMineReportFinal _dom = new ZMCGMineReportFinal();	
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		for (int i = 0 ; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CH_MINEREPORT_FINAL"));//主键
			_dom.setFdMiningId(UserSession.getInSessionProperty("miningCode").toString());	//矿井id
			_dom.setFdMiningName(UserSession.getInSessionProperty("miningName").toString());// 矿井名称
			_dom.setFdReportDepartment(Department.getInstance().getDepartmentNameById(_dom.getFdReportDepartmentId()));//部门名称
			_dom.setFdReporterId(UserSession.getUserId().toString());// 汇报人ID
			_dom.setFdReporter(UserSession.getInSessionProperty("UserCname").toString());// 汇报人		
			_dom.setFdVirtualDate(WorkSchedule.getVirtualDate(_dom.getFdCurrentDate(), _dom.getFdWorknumId()));
			
			boolean validate = validate(inInfo, _dom,"ZMCGMineReportFinal.validate", new HashMap<String, String>());
			if(validate) {
				out = this.query(inInfo);
				return out;
			}
			this.dao.insert("ZMCGMineReportFinal.insert",_dom);
		}
		
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;
	}
	
	/**
	 * 修改
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCP70</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-6下午03:58:24 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo out = new EiInfo();
		ZMCGMineReportFinal _dom = new ZMCGMineReportFinal();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			
			_dom.setFdReportDepartment(Department.getInstance().getDepartmentNameById(_dom.getFdReportDepartmentId()));//部门名称
			_dom.setFdReporterId(UserSession.getUserId().toString());// 汇报人ID
			_dom.setFdReporter(UserSession.getInSessionProperty("UserCname").toString());// 汇报人
			_dom.setFdVirtualDate(WorkSchedule.getVirtualDate(_dom.getFdCurrentDate(), _dom.getFdWorknumId()));
			
			Map<String, String>	params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());
			
			boolean validate = validate(inInfo, _dom,"ZMCGMineReportFinal.validate",params);
			if(validate) {
				out = this.query(inInfo);
				return out;
			}
			this.dao.update("ZMCGMineReportFinal.update",_dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;		
	}
	
	/**
	 * 删除
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCP70</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-6下午03:58:35 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo _temp = super.delete(inInfo, "ZMCGMineReportFinal.delete");
		EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 上报
	 * <p>Function: report</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-6下午04:07:27 张志芳
	 *    2016年8月25日17:56:29 王伟 修改
	 *    修改内容为：在上报的同时更新调度生产日报表中的累计值
	 * </p>
	 *
	 */
	public EiInfo report(EiInfo inInfo)
	{
		EiInfo out=new EiInfo(); 
		ZMCGMineReportFinal _dom = new ZMCGMineReportFinal();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			_dom.setFdStatus("上报");
			this.dao.update("ZMCGMineReportFinal.report",_dom);
			
            String fdWorknumId = _dom.getFdWorknumId();
			String fdVirtualDate = _dom.getFdVirtualDate();
			String fdMiningId = _dom.getFdMiningId();
			String fdMiningName = _dom.getFdMiningName();			
			DailyReport.generateMine(fdWorknumId, fdVirtualDate, fdMiningId, fdMiningName);
		}	
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "条记录成功");
		return out;    
	}
	
	/**
	 * 撤回
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCP70</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-6下午03:58:24 张志芳
	 * </p>
	 *
	 */
	public EiInfo goback(EiInfo inInfo) {
		EiInfo out = new EiInfo();
		ZMCGMineReportFinal _dom = new ZMCGMineReportFinal();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			dao.delete("ZMCGMineReportFinal.delete",_dom);
			dao.update("ZMCHMineReport.goback", _dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "条记录成功");
		return out;	
			
	}		

	/**
	 * 校验是否重复
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-16下午12:48:34 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes" })
	private boolean validate(EiInfo inInfo, ZMCGMineReportFinal _dom, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		
		params.put("fdWorknumId", _dom.getFdWorknumId());
		params.put("fdMiningId", _dom.getFdMiningId());
		params.put("fdCurrentDate", _dom.getFdCurrentDate());
		params.put("fdReportDepartmentId", _dom.getFdReportDepartmentId());
		params.put("fdProjectId", _dom.getFdProjectId());
		List result = dao.query("ZMCGMineReportFinal.validate", params);
		if(result != null && result.size() > 0) {
			inInfo.setStatus(-1);
			String msg = "该采煤进度汇报信息已存在！";
			inInfo.setMsg(msg);
			return true;
		}
		return false;
	}	
}